﻿using System;
using System.Data;
using System.Text;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using WeifenLuo.WinFormsUI.Base;

namespace HRM
{
    public partial class FormContractType : FormBaseDataGrid
    {
        private Ctrl_General mCtrlGeneral;
        private DataSet mdsContractType;
        private SqlDataAdapter mdaContractTypeUpdate;

        public FormContractType()
        {
            InitializeComponent();
        }

        protected override void BasicForm_Load(object sender, EventArgs e)
        {
            base.BasicForm_Load(sender, e);
        }

        protected override void BaseForm_Activated(object sender, System.EventArgs e)
        {
            base.BaseForm_Activated(sender, e);
        }

        protected override void BaseForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            base.BaseForm_KeyDown(sender, e);
        }

        protected override void BasicForm_Closing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            base.BasicForm_Closing(sender, e);
        }

        protected override void LoadMasterData()
        {
            try
            {
                btnCancel = btncancel;
                btnOk = btnok;
                lblStatus = lblstatus;
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot load Contract Type Form'. Error: '" + ex.Message + "'", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void LoadData()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                if (mCtrlGeneral == null) mCtrlGeneral = new Ctrl_General();

                string ls_SQL = "Select ID_ContractType, ContractType_Name, CreatedUser, UpdatedUser, CreatedDate, UpdatedDate "
                    + "From HRM_ContractType ";

                mdsContractType = mCtrlGeneral.GetSelect(ls_SQL, "HRM_ContractType");
                mCtrlGeneral.GetAllData(out mdaContractTypeUpdate, "HRM_ContractType", "");

                if (mdsContractType == null) return;
                //mdbg_dataGrid.AutoGenerateColumns = false;
                mdbg_dataGrid.DataSource = mdsContractType;
                mdbg_dataGrid.DataMember = "HRM_ContractType";


                mRowManager = (CurrencyManager)mdbg_dataGrid.BindingContext[mdsContractType, "HRM_ContractType"];
                // How to refresh the status of the row position?Ordinal_Number
                mRowManager.PositionChanged += new EventHandler(RowMaster_Changed);

                base.LoadMasterData();
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Contract Type Form'. Error: '" + ex.Message + "'", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Ctrl_LOG.Print("Contract Type form", "LoadData() method", "Error: " + ex.Message);
                Cursor = Cursors.Default;
            }
        }

        public override void save()
        {
            if (!mdbg_dataGrid.EndEdit()) return;
            try
            {
                if (mdsContractType == null) return;
                mRowManager.EndCurrentEdit();
                if (!mdsContractType.HasChanges())
                {
                    base.save();
                    return;
                }
                DataSet ldsHasChanged = mdsContractType.GetChanges();
                foreach (DataRow dr in ldsHasChanged.Tables[0].Rows)
                {
                    if (dr.RowState == DataRowState.Added)
                    {
                        dr["CreatedDate"] = DateTime.Now;
                        dr["CreatedUser"] = mMainForm.ms_UserID;
                    }
                    else if (dr.RowState == DataRowState.Modified)
                    {
                        dr["UpdatedDate"] = DateTime.Now;
                        dr["UpdatedUser"] = mMainForm.ms_UserID;
                    }

                    if (dr["ID_ContractType"].ToString().Equals(""))
                        dr["ID_ContractType"] = DBNull.Value;
                }
                string ls_ColName = "", ls_TableName = "";
                string ls_Error = mCtrlGeneral.Update(mdaContractTypeUpdate, ldsHasChanged, "HRM_ContractType", out ls_ColName, out ls_TableName);
                if (ls_Error == "")
                {
                    mdsContractType.AcceptChanges();
                    int li_pos = 0;
                    if (GetCurrentAction() == MODES.MODIFY) li_pos = mRowManager.Position;
                    LoadData();
                    if (li_pos > 0) mRowManager.Position = li_pos;
                    base.save();
                }
                else
                {
                    MessageBox.Show("Cannot save'. Error: '" + ls_Error + "'", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("DB Error: " + ex.Message, ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public override void cancel()
        {
            mdbg_dataGrid.Focus();
            if (mdsContractType == null) return;
            mRowManager.EndCurrentEdit();
            mdsContractType.RejectChanges();
            base.cancel();
        }

        public override void add()
        {
            mdbg_dataGrid.Focus();
            //mdbg_dataGrid.AllowAddNew = true;
            base.add();
        }

        public override void modify()
        {
            base.modify();
            mdbg_dataGrid.Columns["ID_ContractType"].ReadOnly = true;
        }

        public override void delete()
        {
            base.delete();
            if (!mb_AcceptDelete) return;
            DataSet ldsDelete = (DataSet)mdbg_dataGrid.DataSource;
            DataRow ldrDelete = ldsDelete.Tables[0].Rows[mRowManager.Position];
            ldrDelete.Delete();
            string lsColName = "", lsTableName = "";
            string lsReturnMessage = mCtrlGeneral.Update(mdaContractTypeUpdate, ldsDelete, "HRM_ContractType", out lsColName, out lsTableName);
            if (lsReturnMessage != "")
            {
                MessageBox.Show("Cannot delete'. Error: '" + lsReturnMessage + "'", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                ldsDelete.RejectChanges();
                ldsDelete.AcceptChanges();
            }
        }

        private void btnok_Click(object sender, System.EventArgs e)
        {
            save();
        }

        private void btncancel_Click(object sender, System.EventArgs e)
        {
            cancel();
        }

        protected override void DeleteRowGrid()
        {
        }

    }
}
